package com.buddy.learn.javasource._thread;


/**
 * @author Buddy
 * @date 2021/4/18
 */
public class PrintABCUseWaitNotify {

    private int times;
    private int state;

    public PrintABCUseWaitNotify(int times) {
        this.times = times;
    }

    public void printA() {
        try {
            print("A", 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void printB() {
        try {
            print("B", 1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void printC() {
        try {
            print("C", 2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void print(String value, int targetStat) throws InterruptedException {
        for (int i = 0; i < times; i++) {
            synchronized (this) {
                while (state % 3 != targetStat) {
                    this.wait();
                }
                state++;
                System.out.println(i + ":" + value);
                this.notifyAll();
            }
        }
    }

}
